//
//  AdviceViewController.m
//  QuickCare
//
//  Created by lidabiao on 16/7/14.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "AdviceViewController.h"
#import "AdviceTextView.h"
#import <Masonry/Masonry.h>
#import <SVProgressHUD/SVProgressHUD.h>

@interface AdviceViewController ()

@property (nonatomic, weak) AdviceTextView *adviceTextView;

@property (nonatomic, weak) UIButton *doneButton;

@end

@implementation AdviceViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    //添加按钮
    UIButton *doneButton = [[UIButton alloc] init];
    [doneButton setTitle:@"提交" forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneButton sizeToFit];
    [doneButton setBackgroundColor:[UIColor colorWithRed:47 / 256.0 green:198 / 256.0 blue:197 / 256.0 alpha:0.7]];
    doneButton.layer.cornerRadius = 3;
    doneButton.layer.masksToBounds = YES;
    [doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:doneButton];
    self.doneButton = doneButton;
    
    //添加textView
    AdviceTextView *adView = [[AdviceTextView alloc] initWithFrame: CGRectZero];
    [self.view addSubview:adView];
    self.adviceTextView = adView;
    
    //添加自动布局
    [self autoLayout];
}

///  布局
-(void)autoLayout
{
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.centerY.equalTo(self.view);
        make.centerX.equalTo(self.view).multipliedBy(1.8);
    }];
    
    [self.adviceTextView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.mas_topLayoutGuideBottom).offset(10);
         make.left.equalTo(self.view).offset(10);
         make.right.equalTo(self.view).offset(-10);
         make.bottom.equalTo(self.doneButton.mas_top);
     }];
}

///  按钮点击方法
-(void)doneButtonClick
{
    if (!self.adviceTextView.hasText)
    {
        [SVProgressHUD showErrorWithStatus:@"没有可提交的内容"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
                       {
                           [SVProgressHUD dismiss];
                       });
        
        return;
    }
    
    [SVProgressHUD showWithStatus:@"提交成功"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
    {
        [SVProgressHUD dismiss];
    });
}

@end
